package com.kaoshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.*;
import com.kaoshi.service.*;
import org.springframework.beans.BeanUtils;
import com.kaoshi.entity.Team;
import com.kaoshi.service.ITeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/team")
public class TeamController {
    @Autowired
    private ITeamService teamService;
    @Autowired
    private IInstitutionService institutionService;
    @Autowired
    private IBaoService baoService;
    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private ITDService itdService;
    @Autowired
    private ITBService itbService;
    @Autowired
    private IShenqingService shenqingService;

    @RequestMapping("bao")
    public Result bao(Integer[] ids) {
        LambdaQueryWrapper<Bao> qw = new LambdaQueryWrapper<>();
        qw.in(Bao::getId, Arrays.asList(ids));
        return Result.ok(baoService.list(qw));
    }

    @RequestMapping("findjigou")
    public Result finfindjigoudAll() {
        return Result.ok(institutionService.list());
    }

    @RequestMapping("findAll")
    public Result findAll(Integer current, Integer pageSize, Integer id, Integer zt,String name, @RequestBody Team team) {
        if (zt == 1) {
            Page<Team> page = new Page<>(current, pageSize);
            return Result.ok(teamService.findpage(page, team, id, name));
        } else {
            Page<Team> page = new Page<>(current, pageSize);
            return Result.ok(teamService.findpage1(page, team, id));
        }

    }

    @RequestMapping("TfindAll")
    public List<Team> TfindAll(){
        return teamService.list();
    }
//    @RequestMapping("/findPage")
//    public Result findPage(Integer current, Integer pageSize, @RequestBody Team team){
//        Page page=institutionService.findPage(current,pageSize,team);
//        return Result.ok(page);
//    }

    @RequestMapping("save")
    @Transactional
    public Result save(Integer[] baoid, Integer[] doctorid, @RequestBody Team team,String name) {

        if (team.getId() == null) {
            Doctor doctor = doctorService.list(new LambdaQueryWrapper<Doctor>().eq(Doctor::getId, doctorid[0])).get(0);
            team.setCjname(doctor.getName());
            team.setDid(doctorid[0]);
            team.setCjtime(LocalDateTime.now());
            team.setDname(name);
            teamService.save(team);

            TD td = new TD();
            td.setTid(team.getId());
            td.setDid(doctorid[0]);
            td.setTz(doctorid[0]);
            itdService.save(td);

            for (Integer i : baoid) {
                TB tb = new TB();
                tb.setTid(team.getId());
                tb.setBid(i);
                itbService.save(tb);
            }
            return Result.ok();
        }
        return Result.fail();
    }

    @RequestMapping("findtuan")
    public Result findtuan(@RequestBody Team team) {
        if (team.getTeamname() != null) {
            return Result.ok(teamService.list(new LambdaQueryWrapper<Team>().like(Team::getTeamname, team.getTeamname())));
        } else {
            return Result.ok(teamService.list());
        }
    }
    @RequestMapping("jiesan")
    public Result jiesan(Integer id) {
        LambdaQueryWrapper<Team> qw = new LambdaQueryWrapper<>();
        List<Team> list = teamService.list(qw.eq(Team::getId, id));
        if (list.size()>0){
            list.get(0).setLj(0);
            teamService.updateById(list.get(0));
        }
        LambdaQueryWrapper<TD> w = new LambdaQueryWrapper<>();
        w.eq(TD::getTid,id);
        List<TD> list1 = itdService.list(w);
        for (TD td : list1) {
            td.setDel(0);
            itdService.updateById(td);
        }
        return Result.ok("解散成功！");
    }
    @RequestMapping("t")
    public Result t(Integer id){
        return Result.ok(teamService.list(new LambdaQueryWrapper<Team>().eq(Team::getId, id)).get(0));
    }
    @RequestMapping("teambao")
    public Result teambao(Integer tid){
        LambdaQueryWrapper<TB> qw = new LambdaQueryWrapper<>();
        qw.eq(TB::getTid,tid);
        List<TB> list = itbService.list(qw);
        if (list.size()>0){
            List<Integer> l = new ArrayList<>();
            list.forEach(td->{
                l.add(td.getBid());
            });
            return Result.ok(baoService.list(new LambdaQueryWrapper<Bao>().in(Bao::getId,l)));
        }else {
            return Result.ok();
        }
    }
    @RequestMapping("teamdoctor")
    public Result teamdoctor(Integer tid){
        LambdaQueryWrapper<TD> qw = new LambdaQueryWrapper<>();
        qw.eq(TD::getTid,tid);
        List<TD> list = itdService.list(qw);
        List<Integer> l = new ArrayList<>();
        list.forEach(td->{
            l.add(td.getDid());
        });
        return Result.ok(doctorService.list(new LambdaQueryWrapper<Doctor>().in(Doctor::getId,l)));
    }
    @RequestMapping("sqrt")
    public Result sqrt(Integer teamid, Integer sqrid) {
        List<TD> list = itdService.list(new LambdaQueryWrapper<TD>().eq(TD::getTid, teamid).eq(TD::getDid, sqrid));
        if (list.size() > 0) {
            return Result.ok("你已经在团队！");
        }
        LambdaQueryWrapper<Shenqing> qw = new LambdaQueryWrapper<Shenqing>().eq(Shenqing::getDid, sqrid).eq(Shenqing::getTid, teamid);
        if (shenqingService.list(qw).size() > 0) {
            return Result.ok("已经申请过了！");
        }

        Shenqing sq = new Shenqing();
        sq.setTid(teamid);
        sq.setDid(sqrid);
        shenqingService.save(sq);
        return Result.ok("入团申请成功！");
    }
    @RequestMapping("tui")
    public Result tui(Integer teamid, Integer doctor) {
        LambdaQueryWrapper<TD> qw = new LambdaQueryWrapper<>();
        qw.eq(TD::getDid,doctor).eq(TD::getTid,teamid);
        List<TD> list = itdService.list(qw);
        if (list.size()>0){
            list.get(0).setDel(0);
            itdService.updateById(list.get(0));
            return Result.ok("退队成功");
        }
        return Result.ok();
    }
    @RequestMapping("update")
    public Result update(@RequestBody Team team) {
        teamService.updateById(team);
        return Result.ok("修改成功");
    }
    @RequestMapping("sq")
    public Result sq(Integer tid){
        LambdaQueryWrapper<Shenqing> q1 = new LambdaQueryWrapper<>();
        q1.eq(Shenqing::getTid,tid);
        List<Shenqing> list = shenqingService.list(q1);
        ArrayList<Doctor> doctors = new ArrayList<>();
        for (Shenqing shenqing : list) {
            QueryWrapper<Doctor> q2 = new QueryWrapper<>();
            q2.eq("id",shenqing.getDid());
            Doctor doctor = doctorService.list(q2).get(0);
            doctor.setSqtime(shenqing.getSqtime());
            doctor.setZt(shenqing.getZt());
            doctors.add(doctor);
        }
        System.out.println(doctors);
        return Result.ok(doctors);
    }
    @RequestMapping("tg")
    public Result tg(Integer did,Integer tid){
        LambdaQueryWrapper<Shenqing> q1 = new LambdaQueryWrapper<>();
        q1.eq(Shenqing::getDid,did).eq(Shenqing::getTid,tid);
        Shenqing shenqing = shenqingService.list(q1).get(0);
        shenqing.setZt(1);
        shenqingService.updateById(shenqing);

//        LambdaQueryWrapper<Doctor> q2 = new LambdaQueryWrapper<>();
//        q2.eq(Doctor::getId,did);
//        Doctor doctor = doctorService.list(q2).get(0);
        TD td = new TD();
        td.setDid(did);
        td.setTid(tid);
        td.setTz(0);
        itdService.save(td);

        return Result.ok();
    }
    @RequestMapping("jj")
    public Result jj(Integer did,Integer tid){
        LambdaQueryWrapper<Shenqing> q1 = new LambdaQueryWrapper<>();
        q1.eq(Shenqing::getDid,did).eq(Shenqing::getTid,tid);
        Shenqing shenqing = shenqingService.list(q1).get(0);
        shenqing.setZt(0);
        shenqingService.updateById(shenqing);

//        LambdaQueryWrapper<Doctor> q2 = new LambdaQueryWrapper<>();
//        q2.eq(Doctor::getId,did);
//        Doctor doctor = doctorService.list(q2).get(0);
//        TD td = new TD();
//        td.setDid(did);
//        td.setTid(tid);
//        td.setTz(0);
//        itdService.save(td);

        return Result.ok();
    }
}
